//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "ShipmentsReport.h"
#include "StringUtilities.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

TShipmentReport :: TShipmentReport( TDatabase * database)
{
   qryShipments = new TQuery( NULL);
   qryShipments->DatabaseName = database->DatabaseName;

   BuildQuery();
}

TShipmentReport :: ~TShipmentReport()
{
   delete qryShipments;
}

void TShipmentReport :: BuildQuery()
{
   qryShipments->SQL->Add( "SELECT * FROM SHIPMENTS");
   qryShipments->SQL->Add( "WHERE ( SHIPMENT_DATE >= :prmFromDate)");
   qryShipments->SQL->Add( "AND ( SHIPMENT_DATE < :prmToDate)");
   qryShipments->SQL->Add( "AND ( PROVIDER_ID = :prmProviderID)");     
   qryShipments->SQL->Add( "ORDER BY SHIPMENT_DATE");
}

TQuery * TShipmentReport :: GetQuery()
{
   return qryShipments;
}

void TShipmentReport :: Produce( int year, int month, __int64 providerID)
{
   TDateTime fromDate;
   TDateTime toDate;

   this->year = year;
   fromDate   = EncodeDate( year, month, 1);
   toDate     = IncMonth( fromDate, 1);

   qryShipments->ParamByName( "prmFromDate")->AsDate      = fromDate;
   qryShipments->ParamByName( "prmToDate")->AsDate        = toDate;
   qryShipments->ParamByName( "prmProviderID")->AsInteger = providerID;

   qryShipments->Open();
}

void TShipmentReport :: Close()
{
   qryShipments->Close();
}

//------------------------------------------------------------------------------
__fastcall TfrmShipmentsReport::TfrmShipmentsReport(TComponent* Owner, TDatabase* dbMain)
        : TForm(Owner)
{
   shipmentReport = new TShipmentReport( dbMain);
   totalAmount = 0;
}

void __fastcall TfrmShipmentsReport::FormDestroy(TObject *Sender)
{
   delete shipmentReport;
}

void TfrmShipmentsReport :: Preview( int year, int month, __int64 providerID)
{
   shipmentReport->Produce( year, month, providerID);

   try
   {
      qrShipments->DataSet = shipmentReport->GetQuery();

      txtShipmentNoValue->DataSet = shipmentReport->GetQuery();
      txtShipmentNoValue->DataField = "SHIPMENT_NUMBER";
      txtDateValue->DataSet = shipmentReport->GetQuery();
      txtDateValue->DataField = "SHIPMENT_DATE";
      txtAmountValue->DataSet = shipmentReport->GetQuery();
      txtAmountValue->DataField = "AMOUNT";

      qrShipments->Preview();
   }
   __finally
   {
      shipmentReport->Close();
   }
}

void __fastcall TfrmShipmentsReport::bndSummaryBeforePrint(
      TQRCustomBand *Sender, bool &PrintBand)
{
   lblTotalAmountValue->Caption  = CurrencyString + " " + FloatToStr( totalAmount);
   totalAmount = 0;
}


void __fastcall TfrmShipmentsReport::lblAmountValuePrint(TObject *sender,
      AnsiString &Value)
{
   totalAmount += ( double)shipmentReport->GetQuery()->FieldByName( "AMOUNT")->AsCurrency;        
}

